/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.tom.social.analyzer.gui.options;

import javax.swing.DefaultListModel;
import org.openide.util.NbPreferences;

final class MainPanel extends javax.swing.JPanel {

	private final MainOptionsPanelController controller;

	MainPanel(MainOptionsPanelController controller) {
		this.controller = controller;
		initComponents();
		// TODO listen to changes in form fields and call controller.changed()
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        serverPanel = new javax.swing.JPanel();
        serverListScrollPane = new javax.swing.JScrollPane();
        serverList = new javax.swing.JList();
        serverTextField = new javax.swing.JTextField();
        serverAddButton = new javax.swing.JButton();
        serverRemoveButton = new javax.swing.JButton();

        serverPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), org.openide.util.NbBundle.getMessage(MainPanel.class, "MainPanel.serverPanel.border.title"))); // NOI18N

        serverList.setModel(new DefaultListModel());
        serverList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        serverListScrollPane.setViewportView(serverList);

        serverTextField.setText(org.openide.util.NbBundle.getMessage(MainPanel.class, "MainPanel.serverTextField.text")); // NOI18N

        serverAddButton.setLabel(org.openide.util.NbBundle.getMessage(MainPanel.class, "MainPanel.serverAddButton.label")); // NOI18N
        serverAddButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                serverAddButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(serverRemoveButton, org.openide.util.NbBundle.getMessage(MainPanel.class, "MainPanel.serverRemoveButton.text_1")); // NOI18N
        serverRemoveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                serverRemoveButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout serverPanelLayout = new javax.swing.GroupLayout(serverPanel);
        serverPanel.setLayout(serverPanelLayout);
        serverPanelLayout.setHorizontalGroup(
            serverPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(serverPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(serverPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(serverListScrollPane, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, serverPanelLayout.createSequentialGroup()
                        .addComponent(serverTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(serverAddButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(serverRemoveButton)))
                .addContainerGap(343, Short.MAX_VALUE))
        );
        serverPanelLayout.setVerticalGroup(
            serverPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(serverPanelLayout.createSequentialGroup()
                .addComponent(serverListScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(serverPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(serverTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(serverAddButton)
                    .addComponent(serverRemoveButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(serverPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(serverPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(261, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

	private void serverAddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_serverAddButtonActionPerformed
		String server = serverTextField.getText();
		if(serverList.getModel() instanceof DefaultListModel) {
			if(server != null) {
				if(!server.equals("")) {
					((DefaultListModel) serverList.getModel()).addElement(server);
					serverList.repaint();
				}
			}
		}
	}//GEN-LAST:event_serverAddButtonActionPerformed

	private void serverRemoveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_serverRemoveButtonActionPerformed
		int index = serverList.getSelectedIndex();
		if(serverList.getModel() instanceof DefaultListModel) {
			if(index > 0) {
				((DefaultListModel) serverList.getModel()).removeElementAt(index);
				serverList.repaint();
			}
		}
	}//GEN-LAST:event_serverRemoveButtonActionPerformed

	void load() {
		String servers = NbPreferences.forModule(MainPanel.class).get("networkServers", null);
		if(servers != null) {
			String[] serversArray = servers.split(";");
			if(serverList.getModel() instanceof DefaultListModel) {
				DefaultListModel model = (DefaultListModel) serverList.getModel();
				for(String server : serversArray) {
					model.addElement(server);
				}
			}
		}
	}

	void store() {
		StringBuilder servers = new StringBuilder(serverList.getModel().getSize());
		for(int i = 0; i < serverList.getModel().getSize() - 1; i++) {
			servers.append(serverList.getModel().getElementAt(i));
			servers.append(";");
		}
		servers.append(serverList.getModel().getElementAt(serverList.getModel().getSize() - 1));
		NbPreferences.forModule(MainPanel.class).put("networkServers", servers.toString());
	}

	boolean valid() {
		// TODO check whether form is consistent and complete
		return true;
	}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton serverAddButton;
    private javax.swing.JList serverList;
    private javax.swing.JScrollPane serverListScrollPane;
    private javax.swing.JPanel serverPanel;
    private javax.swing.JButton serverRemoveButton;
    private javax.swing.JTextField serverTextField;
    // End of variables declaration//GEN-END:variables
}
